package com.zw.yzk.kabu.view.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.widget.EditText;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.data.cache.db.DataBaseManager;
import com.zw.yzk.kabu.data.cache.db.User;
import com.zw.yzk.kabu.internal.component.DaggerModifyPasswordComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.ModifyPasswordModule;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.presenter.ModifyPasswordPresenter;
import com.zw.yzk.kabu.utils.GetRsaString;
import com.zw.yzk.kabu.view.view.ModifyPasswordView;
import com.zw.yzk.kabu.view.widget.LoginStateInvalidDialog;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by zhanwei on 2016/12/25.
 */

public class ModifyPasswordActivity extends BaseActivity implements ModifyPasswordView {

    @Bind(R.id.original_password)
    EditText originalPassword;
    @Bind(R.id.new_password)
    EditText newPassword;
    @Bind(R.id.new_password_again)
    EditText newPasswordAgain;

    @Inject
    ModifyPasswordPresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_modify_password;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setBackgroundColor(ContextCompat.getColor(this, R.color.transparent))
                .setTitle(getResources().getString(R.string.modify_password))
                .build();
    }

    @Override
    public void initComponent() {
        DaggerModifyPasswordComponent
                .builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .modifyPasswordModule(new ModifyPasswordModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
    }

    @Override
    public void modifyPasswordSuccess() {
        ToastManager.getInstance().showToast(this, getResources().getString(R.string.modify_password_success));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 500);
    }

    @OnClick(R.id.modify_password)
    public void resetPassword() {
        String oP = originalPassword.getText().toString();
        String nP = newPassword.getText().toString();
        String nPA = newPasswordAgain.getText().toString();
        if (TextUtils.isEmpty(oP)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.please_input_original_password));
        } else if (TextUtils.isEmpty(nP)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.please_input_new_password));
        }  else if(nP.length() < 6 || nP.length() > 20) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.password_limit));
        } else if (TextUtils.isEmpty(nPA)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.please_input_new_password_again));
        } else if (!nP.equals(nPA)) {
            ToastManager.getInstance().showToast(this, getResources().getString(R.string.twice_password_not_match));
        } else {
            User user = DataBaseManager.getInstance().getLoginUser();
            if(user != null && user.isLogin()) {
                modifyPassword(user.getPhone(), oP, nPA);
            }else {
                new LoginStateInvalidDialog().showInvalidDialog(this, getResources().getString(R.string.invalid_token));
            }
        }
    }

    /**
     * 修改密码
     *
     * @param account          账号
     * @param originalPassword 原密码
     * @param newPassword      新密码
     */
    private void modifyPassword(String account, String originalPassword, String newPassword) {
        presenter.modifyPassword(account, new GetRsaString(this, originalPassword).getRsa(), new GetRsaString(this, newPassword).getRsa());
    }


}
